#
#
"""
Workflow for Online Community Survey types
"""
__version__ = "$Revision: 1.1.1.1 $"[11:-2]

from Products.CMFCore.WorkflowTool import addWorkflowFactory
from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
from Products.ArchSurvey.Permissions import *

def setup_surveyWorkflow(wf):
	"..."
	wf.setProperties(title='Survey Workflow [OCWorkflows]')

	for s in ['editing', 'open', 'closed']:
		wf.states.addState(s)
	for t in ['open', 'close', 'edit']:
		wf.transitions.addTransition(t)
	for v in ['action', 'review_history', 'actor', 'comments', 'time']:
		wf.variables.addVariable(v)

	for p in (EDIT_CONTENT_PERMISSION,
			  VIEW_CONTENT_PERMISSION,
			  VIEW_RESULTS,
			  TAKE_SURVEY,
			  ACCESS_CONTENTS):
		wf.addManagedPermission(p)
		
	## Initial State
	wf.states.setInitialState('editing')
	
	## State Variable
	wf.variables.setStateVar('review_state')

	## States initialization	
	sdef = wf.states['editing']
	sdef.setProperties(title="Editing",
	                   transitions=('open',))
	sdef.setPermission(EDIT_CONTENT_PERMISSION, 0, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(VIEW_CONTENT_PERMISSION, 0, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(ACCESS_CONTENTS, 0, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(VIEW_RESULTS, 0, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(TAKE_SURVEY, 0, ['Manager', 'Facilitator','Owner'])
	
	sdef = wf.states['open']
	sdef.setProperties(title="Open",
	                   transitions=('close','edit'))
	sdef.setPermission(EDIT_CONTENT_PERMISSION, 0, ['Manager', 'Facilitator'])
	sdef.setPermission(VIEW_CONTENT_PERMISSION, 1, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(ACCESS_CONTENTS, 1, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(VIEW_RESULTS, 0, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(TAKE_SURVEY, 1, ['Manager', 'Facilitator','Owner','Member'])
	
	sdef = wf.states['closed']
	sdef.setProperties(title="Closed",
		               transitions=('open','edit'))
	sdef.setPermission(EDIT_CONTENT_PERMISSION, 0, [])
	sdef.setPermission(VIEW_CONTENT_PERMISSION, 1, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(ACCESS_CONTENTS, 1, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(VIEW_RESULTS, 0, ['Manager', 'Facilitator','Owner'])
	sdef.setPermission(TAKE_SURVEY, 0, [])

	## Transitions initialization
	tdef = wf.transitions['open']
	tdef.setProperties(title="""Open""",
					   new_state_id="""open""",
					   trigger_type=1,
					   script_name="""""",
					   after_script_name="""""",
					   actbox_name="""Open""",
					   actbox_url="""""",
					   actbox_category="""workflow""",
					   props={'guard_roles':'Manager;Facilitator;Owner'}
					   )

	tdef = wf.transitions['close']
	tdef.setProperties(title="""Close""",
					   new_state_id="""closed""",
					   trigger_type=1,
					   script_name="""""",
					   after_script_name="""""",
					   actbox_name="""Close""",
					   actbox_url="""""",
					   actbox_category="""workflow""",
					   props={'guard_roles':'Manager;Facilitator;Owner'}
					   )

	tdef = wf.transitions['edit']
	tdef.setProperties(title="""Edit""",
					   new_state_id="""editing""",
					   trigger_type=1,
					   script_name="""""",
					   after_script_name="""""",
					   actbox_name="""Edit""",
					   actbox_url="""""",
					   actbox_category="""workflow""",
					   props={'guard_roles':'Manager;Facilitator'}
					   )
		
def create_surveyWorkflow(id):
	"..."
	ob = DCWorkflowDefinition(id)
	setup_surveyWorkflow(ob)
	return ob

survey_workflow_id='survey_workflow'
survey_workflow='survey_workflow (Survey Workflow [Survey])'

addWorkflowFactory(create_surveyWorkflow,
				   id='survey_workflow',
				   title='Survey Workflow [Survey]')	
